using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using System.Drawing;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectTinTuc()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectTinTucPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListTinTuc", CreateViewModel(data));
		}

		public ActionResult SelectTinTucByIDLoaiTinTuc(int idloaitintuc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiTinTuc = idloaitintuc,
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return View("SelectTinTuc", CreateViewModel(data));
		}

		public ActionResult SelectTinTucByIDLoaiTinTucPaging(int idloaitintuc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiTinTuc = idloaitintuc,
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListTinTuc", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteTinTuc(int id)
		{
			var deleteItem = _repTinTuc.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idloaitintuc = deleteItem.IDLoaiTinTuc;
			var result = _repTinTuc.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repTinTuc.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfTinTuc(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_TINTUC,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateTinTuc(FormCollection forms)
		{
			string id = forms.GetValues("TinTuc_ID")[0];
			string idloaitintuc = forms.GetValues("TinTuc_IDLoaiTinTuc")[0];
			string tieude = forms.GetValues("TinTuc_TieuDe")[0];
			string noidung = forms.GetValues("TinTuc_NoiDung")[0];
            string noidungrutgon = StringHelper.Truncate(forms.GetValues("TinTuc_NoiDungRutGon")[0], WebConfiguration.TinTucSortDescription);
            string nguon = forms.GetValues("TinTuc_Nguon")[0];
			var file = Request.Files["TinTuc_HinhMinhHoa"];

			string refName = "";
			var editItem = _repTinTuc.SelectByID(int.Parse(id));
			editItem.IDLoaiTinTuc = int.Parse(idloaitintuc.Replace(".", "").Replace(",", ""));
			editItem.TieuDe = tieude;
			editItem.NoiDung = noidung;
			editItem.NoiDungRutGon = noidungrutgon;
            editItem.Nguon = nguon;
			var result = false;
			
            if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repTinTuc.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_TINTUC,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfTinTuc", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertTinTuc(FormCollection forms)
		{
			string idloaitintuc = forms.GetValues("TinTuc_IDLoaiTinTuc")[0];
			string tieude = forms.GetValues("TinTuc_TieuDe")[0];
			string noidung = forms.GetValues("TinTuc_NoiDung")[0];
            string noidungrutgon = StringHelper.Truncate(forms.GetValues("TinTuc_NoiDungRutGon")[0], WebConfiguration.TinTucSortDescription);
			string tennguoidang = forms.GetValues("TinTuc_TenNguoiDang")[0];
            string nguon = forms.GetValues("TinTuc_Nguon")[0];
			var file = Request.Files["TinTuc_HinhMinhHoa"];

			string refName = string.Empty;
			var newItem = new TinTuc
			{
				IDLoaiTinTuc = int.Parse(idloaitintuc),
				TieuDe = tieude,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				NgayDang = DateTime.Now,
				TenNguoiDang = tennguoidang,
				HinhMinhHoa = "noimages.jpg",
                Nguon = nguon
			};
			var result = _repTinTuc.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
					if (result)
					{
						var addedItem = _repTinTuc.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repTinTuc.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDLoaiTinTuc = int.Parse(idloaitintuc),
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectTinTuc", CreateViewModel(data));
		}
	}
}